﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Windows.Forms;
using PluginInterface;
using System.Drawing;
using Qurihara;
using System.Collections;
using System.Xml;
using Qurihara.Anm;
using System.IO;
using Qurihara.QBasic;

namespace KotodamaPlugin.MultiViewSnapPlugin
{
	/// <summary>
	/// Class1 の概要の説明です。
	/// </summary>
	public class MultiViewSnapPlugin :IPlugin
	{
        // for new menu
        ToolStripButton tsButtonCapture;
        ToolStripMenuItem tsEdit;
        ToolStripMenuItem tsCapture;
        ToolStripMenuItem tsAll;
        ToolStripButton tsFirst;
        ToolStripButton tsRwd;
        ToolStripComboBox tsComboView;
        ToolStripButton tsFwd;
        ToolStripButton tsLast;


		//event for multidisplay
		public event SnapChangedEventHandler SnapChanged=null;
		public delegate void SnapChangedEventHandler(object sender,SnapChangedEventArgs e);
		public class SnapChangedEventArgs : EventArgs
		{
			public SnapChangedEventArgs(int snapIndex)
			{
				SnapIndex = snapIndex;
			}
			public int SnapIndex;
		}

		// NextPrev
		public int magnifySize = 100;
		private ToolStripMenuItem menu;
		private bool enable = false;
		private ButtonAnmer prevButton;
		private ButtonAnmer nextButton;
		private Rectangle prevNomalRect,prevMagedRect;
		private Rectangle nextNomalRect,nextMagedRect;

		internal ViewSnapList viewSnapList;
//		public ViewSnapList ViewSnapList
//		{
//			get{return viewSnapList;}
//		}
		internal ViewEditForm viewEditForm;

		internal ToolBar toolBar;
		protected ToolBarButton buttonCapture;
		protected ToolBarButton buttonEdit;
		protected ToolBarButton buttonAll;
		protected ToolBarButton buttonOrig;
		protected ToolBarButton buttonPrev;
		protected ToolBarButton buttonNext;
		protected ToolBarButton buttonFocus;
		protected System.Windows.Forms.ComboBox viewComboBox;
		protected int currentViewIndex;

		protected ToolBarButton sepa;

		internal ToolBarDock.ToolBarDockHolder holder = null;

		public MultiViewSnapPlugin()
		{

			// NextPrev
			menu = new ToolStripMenuItem("<>");
			menu.Click += new EventHandler(MenuClick);

			// viewSnap
			//viewSnapList = new ViewSnapList();
			//viewEditForm = new ViewEditForm(viewSnapList);

            buttonCapture = new ToolBarButton(Util.IsPropertyTrue("Base.English") ? "Capture" : "視点記録");
			//buttonCapture.Style = ToolBarButtonStyle.PushButton;
			buttonCapture.Style = ToolBarButtonStyle.DropDownButton;
			
			ContextMenu captureMenu = new ContextMenu();
			buttonCapture.DropDownMenu = captureMenu;
            MenuItem menuAppendCap = new MenuItem(Util.IsPropertyTrue("Base.English") ? "Append" : "最後尾に追加");
            MenuItem menuInsertCap = new MenuItem(Util.IsPropertyTrue("Base.English") ? "Insert" : "挿入");
            MenuItem menuOverwriteCap = new MenuItem(Util.IsPropertyTrue("Base.English") ? "Overwrite" : "上書き");
			buttonCapture.DropDownMenu.MenuItems.Add(menuAppendCap);
			buttonCapture.DropDownMenu.MenuItems.Add(menuInsertCap);
			buttonCapture.DropDownMenu.MenuItems.Add(menuOverwriteCap);
			menuAppendCap.Click +=new EventHandler(menuAppendCap_Click);
			menuInsertCap.Click +=new EventHandler(menuInsertCap_Click);
			menuOverwriteCap.Click +=new EventHandler(menuOverwriteCap_Click);

            buttonEdit = new ToolBarButton(Util.IsPropertyTrue("Base.English") ? "Edit" : "視点編集");
			buttonEdit.Style = ToolBarButtonStyle.PushButton;
            buttonAll = new ToolBarButton(Util.IsPropertyTrue("Base.English") ? "Overview" : "全部視点");
			buttonAll.Style = ToolBarButtonStyle.PushButton;
            buttonOrig = new ToolBarButton(Util.IsPropertyTrue("Base.English") ? "Origin" : "原点視点");
			buttonOrig.Style = ToolBarButtonStyle.PushButton;
			buttonPrev = new ToolBarButton("<<");
			buttonPrev.Style = ToolBarButtonStyle.PushButton;
			buttonFocus = new ToolBarButton("*");
			buttonFocus.Style = ToolBarButtonStyle.PushButton;
			buttonNext = new ToolBarButton(">>");
			buttonNext.Style = ToolBarButtonStyle.PushButton;
			viewComboBox = new ComboBox();
			viewComboBox.Dock = DockStyle.Top;
			viewComboBox.SelectedIndexChanged+=new EventHandler(viewComboBox_SelectedIndexChanged);
			viewComboBox.Click+=new EventHandler(viewComboBox_Click);

			toolBar = new ToolBar();
			toolBar.Text = "Snap";

			toolBar.Buttons.Add(buttonCapture);
			toolBar.Buttons.Add(buttonEdit);
			toolBar.Buttons.Add(buttonAll);
			toolBar.Buttons.Add(buttonOrig);
			toolBar.Buttons.Add(buttonPrev);
			toolBar.Buttons.Add(buttonFocus);
			toolBar.Buttons.Add(buttonNext);
			toolBar.Controls.Add(viewComboBox);
			currentViewIndex = 0;

			sepa = new ToolBarButton();
			sepa.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;

//			// get property for animation effects
//			string s = "";
//			animationInterval = 1000;
//			animationEffect = AnimationEffect.Jump;
//			if (Util.GetStringProperty("MultiViewSnapPlugin.Go.Effect", ref s))
//				animationEffect = AnimationEffect.Nomal;
//			Util.GetInt32Property("MultiViewSnapPlugin.Go.Interval",ref animationInterval);
		}
		protected AnimationEffect animationEffect;
		protected int animationInterval;

		public enum AnimationEffect
		{
			Nomal,
			Jump,
		}
		private void menuAppendCap_Click(object sender, EventArgs e)
		{
			Capture();
		}

		private void menuInsertCap_Click(object sender, EventArgs e)
		{
			InsertCapture();
		}

		private void menuOverwriteCap_Click(object sender, EventArgs e)
		{
			OverwriteCapture();
		}

		private void toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
		{
			if (e.Button == this.buttonCapture)
				Capture();
			if (e.Button == this.buttonEdit)
			{
				viewEditForm.UpdateList();
				viewEditForm.Show();
			}
			if(e.Button == this.buttonAll)
				All_Click();
			if(e.Button == this.buttonOrig)
				Orig_Click();
			if (e.Button == this.buttonPrev)
			{
				GoPrev();
//				currentViewIndex--;
//				bool succeeded = viewSnapList.Go(currentViewIndex);
//				if (!succeeded) currentViewIndex++;
			}
			if (e.Button == this.buttonNext)
			{
				GoNext();
//				currentViewIndex++;
//				bool succeeded = viewSnapList.Go(currentViewIndex);
//				if (!succeeded) currentViewIndex--;
			}
			if (e.Button == this.buttonFocus)
			{
				Focus_Click();
			}
		}
		public void GoPrev()
		{
			currentViewIndex--;
			bool succeeded = viewSnapList.Go(currentViewIndex);
			if (!succeeded) currentViewIndex++;
			if (succeeded && SnapChanged != null) SnapChanged(this,new SnapChangedEventArgs(currentViewIndex));
		}
		public void GoNext()
		{
			currentViewIndex++;
			bool succeeded = viewSnapList.Go(currentViewIndex);
			if (!succeeded) currentViewIndex--;
			if (succeeded && SnapChanged != null) SnapChanged(this,new SnapChangedEventArgs(currentViewIndex));
		}
        private void DialogCapture()
        {
            MessageBox.Show("現在の視点を撮影し記録しました。");
        }
		public void Capture() // append
		{
			int count = viewSnapList.Count+1;
			//ViewSnap vs = new ViewSnap(count.ToString() + "番視点",Host.Kotodama.Presentation.CurrentSlide.View.ViewRectangle,toolBar,Host.Kotodama);
			//ViewSnap vs = new ViewSnap(count.ToString() + "番視点",Host.Kotodama.Presentation.CurrentSlide.View.ViewRectangle,Host.Kotodama);
			ViewSnap vs = new ViewSnap(count.ToString(),Host.Kotodama.Presentation.CurrentSlide.View.ViewRectangle,Host.Kotodama.Presentation.CurrentSlide.View);
			Go(viewSnapList.Add(vs));
			//Save();
			viewEditForm.UpdateList();
            DialogCapture();
		}
		public void InsertCapture()
		{
			int count = viewSnapList.Count+1;
			ViewSnap vs = new ViewSnap(count.ToString(),Host.Kotodama.Presentation.CurrentSlide.View.ViewRectangle,Host.Kotodama.Presentation.CurrentSlide.View);
			viewSnapList.Insert(this.currentViewIndex,vs);
			Go(currentViewIndex);
			viewEditForm.UpdateList();
            DialogCapture();
		}
		public void OverwriteCapture() 
		{
			int count = viewSnapList.Count+1;
			ViewSnap vs = new ViewSnap(count.ToString(),Host.Kotodama.Presentation.CurrentSlide.View.ViewRectangle,Host.Kotodama.Presentation.CurrentSlide.View);
			viewSnapList.Overwrite(this.currentViewIndex,vs);
			Go(currentViewIndex);
			viewEditForm.UpdateList();
            DialogCapture();
		}
		private RectangleElement mask =null;
		private void Orig_Click()
		{
			Qurihara.View view = Host.Kotodama.Presentation.CurrentSlide.View;						
			RectMoveAnm anm = new RectMoveAnm(
				(IAnmElement)Host.Kotodama.Presentation.CurrentSlide.View,
                Host.Kotodama.Presentation.CurrentSlide.View.ViewRectangle,
				new Rectangle(0,0,view.ScreenSize.Width,view.ScreenSize.Height),1600,RectMoveAnm.AnmType.Linear);
			anm.Start();

			// ホワイトアウトトランジションを実験しようとしたが、anmがうまく動かず断念。
//			if(mask == null)
//			{
//				mask = 
////					(RectangleElement)Host.Kotodama.Presentation.CurrentSlide.createRectangle(
////					-1,view.ViewRectangle,Color.Black,Color.White,1);
//					new RectangleElement(Host.Kotodama.Control,-1,
//					Host.Kotodama.Presentation.CurrentSlide.View.ViewRectangle,
//					Color.Black,Color.White,1);
//				Host.Kotodama.Presentation.CurrentSlide.ForeGroundViewDependentElementList.Add(mask);
//				//Host.Kotodama.Presentation.CurrentSlide.BackGroundViewDependentElementList.Add(mask);
//				Host.Kotodama.Control.Refresh();//.Invalidate();
//				RectMoveAnm anm = new RectMoveAnm(mask,
//					new Rectangle(view.ViewRectangle.X-view.ViewRectangle.Width,
//					view.ViewRectangle.Y,
//					view.ViewRectangle.Width,
//					view.ViewRectangle.Height),
//					view.ViewRectangle,
//					1600,
//					RectMoveAnm.AnmType.Cubic);
//	
//				//AppearAnm anm = new AppearAnm(mask,1600);
//				//PlayBackAnm anm = new PlayBackAnm(mask,1600);
//				anm.AnmFinishedHandler+=new EventHandler(anm_AnmFinishedHandler);
//				anm.Start();
//			}
		}
//		private void anm_AnmFinishedHandler(object sender, EventArgs e)
//		{
//			Qurihara.View view = Host.Kotodama.Presentation.CurrentSlide.View;
//			Host.Kotodama.Presentation.CurrentSlide.ForeGroundViewDependentElementList.Remove(mask);
//			//Host.Kotodama.Presentation.CurrentSlide.BackGroundViewDependentElementList.Remove(mask);
//			//Host.Kotodama.Presentation.CurrentSlide.EraseObjects(
//			view.ViewRectangle = 
//				new Rectangle(0,0,view.ScreenSize.Width,view.ScreenSize.Height);
//			mask = null;
//		}

		private void All_Click()
		{
			if (Host.Kotodama.Presentation.CurrentSlide.ElementList.Count > 0)
			{
				Rectangle r = new Rectangle(0,0,0,0);
				Host.Kotodama.Presentation.CurrentSlide.ElementList.GetBoundingBox(ref r);

				float aspect = (float)Host.Kotodama.Presentation.CurrentSlide.View.ScreenSize.Width
					/ (float)Host.Kotodama.Presentation.CurrentSlide.View.ScreenSize.Height;
				Rectangle re = Qurihara.QBasic.DrawingUtil.GetRectangleInAspectOf(aspect,r);

				RectMoveAnm anm = new RectMoveAnm(
					(IAnmElement)Host.Kotodama.Presentation.CurrentSlide.View,
					Host.Kotodama.Presentation.CurrentSlide.View.ViewRectangle,
					re,1600,RectMoveAnm.AnmType.Linear);
				anm.Start();

			}
		}

		private void Focus_Click()
		{
			if (Host.Kotodama.SelectedElementList.Count > 0)
			{
				Rectangle r = new Rectangle(0,0,0,0);
				Host.Kotodama.SelectedElementList.GetBoundingBox(ref r);

				float aspect = (float)Host.Kotodama.Presentation.CurrentSlide.View.ScreenSize.Width
					/ (float)Host.Kotodama.Presentation.CurrentSlide.View.ScreenSize.Height;
				Rectangle re = Qurihara.QBasic.DrawingUtil.GetRectangleInAspectOf(aspect,r);

				RectMoveAnm anm = new RectMoveAnm(
					(IAnmElement)Host.Kotodama.Presentation.CurrentSlide.View,
					Host.Kotodama.Presentation.CurrentSlide.View.ViewRectangle,
					re,1600,RectMoveAnm.AnmType.Linear);
				anm.Start();

			}
		}

		ToolBar defaultBar = null;

		#region IPlugin Members
		
		IPluginHost myPluginHost = null;
		string myPluginName = "MultiViewSnap Plugin";
		string myPluginAuthor = "K.Qurihara";
		string myPluginDescription = "test plugin";
		string myPluginVersion = "1.0.0";
		
		public void Initialize()
		{
			holder = null;
            ToolBarDock.ToolBarManager tbm = (ToolBarDock.ToolBarManager)Host.Kotodama.EnvironmentTable["Base.ToolBarManager"];

			if ( tbm != null)
			{
				tbm.AddControl(toolBar,DockStyle.None);
				holder = tbm.GetHolder(toolBar);
				tbm.ShowControl(holder.Control, true);
				holder.UpdateSize();
				toolBar.ButtonClick +=new ToolBarButtonClickEventHandler(toolBar_ButtonClick);
			}
			else
			{// holderが無い ＞ menuについか
				//Host.Kotodama.Form.Menu.MenuItems.Add(menuItem);
				//enable = true;
				defaultBar = (ToolBar)Host.Kotodama.EnvironmentTable["Base.ToolBar"];
				if(defaultBar != null)
				{
					defaultBar.Buttons.Add(sepa);
					defaultBar.Buttons.Add(buttonPrev);
					defaultBar.Buttons.Add(buttonNext);
					defaultBar.ButtonClick+=new ToolBarButtonClickEventHandler(toolBar_ButtonClick);
				}
			}

			viewEditForm = new ViewEditForm();
			viewEditForm.buttonListNew.Click +=new EventHandler(buttonListNew_Click);
			viewEditForm.buttonListDel.Click +=new EventHandler(buttonListDel_Click);
			ComboBox cb = viewEditForm.comboBoxListName;
			cb.SelectedIndexChanged+=new EventHandler(cb_SelectedIndexChanged);

			//Application.ApplicationExit += new EventHandler(Form_Closing);
			//Host.Kotodama.Form.Closing +=new System.ComponentModel.CancelEventHandler(Form_Closing);
			//Host.Kotodama.Presentation.CurrentSlideChanged+=new EventHandler(Presentation_CurrentSlideChanged);
            Host.Kotodama.Presentation.CurrentSlide.SlideLoaded += new EventHandler(Presentation_CurrentSlideChanged);
			Presentation_CurrentSlideChanged(this,new EventArgs());
			Host.Kotodama.KeyboardUpEventHandler+=new Qurihara.Keyboard.KeyboardUpEventHandler(Kotodama_KeyboardUpEventHandler);

			// NextPrev
            prevButton = new ButtonAnmer();
            prevButton.Button.BackColor = Color.Beige;
            prevButton.Button.Text = Util.IsPropertyTrue("Base.English") ? "Prev." : "一つ前";
            prevButton.Button.Size = new Size(50, 50);
            prevButton.Click += new EventHandler(PrevClick);

            nextButton = new ButtonAnmer();
            nextButton.Button.BackColor = Color.Beige;
            nextButton.Button.Text = Util.IsPropertyTrue("Base.English") ? "Next" : "次";
            nextButton.Button.Size = new Size(50, 50);
            nextButton.Click += new EventHandler(NextClick);

			Host.Kotodama.Control.Controls.Add(prevButton.Button);
			Host.Kotodama.Control.Controls.Add(nextButton.Button);
			SetButtonLocation();


            //ToolStrip main = (ToolStrip)Host.Kotodama.EnvironmentTable["Base.ToolStripMainMenu"];
            //if (main != null)
            //{
            //    main.Items.Add(menu);
            //}

			prevButton.Button.Visible = enable;
			nextButton.Button.Visible = enable;

            // for new menu
            tsButtonCapture = (ToolStripButton)Host.Kotodama.EnvironmentTable["Base.toolStripButtonCapture"];
            if (tsButtonCapture != null)
            {
                tsButtonCapture.Click += new EventHandler(ts_Click);
            }
            tsEdit = (ToolStripMenuItem)Host.Kotodama.EnvironmentTable["Base.再生編集ToolStripMenuItem"];
            if (tsEdit != null)
            {
                tsEdit.Click +=new EventHandler(ts_Click);
            }
            tsCapture = (ToolStripMenuItem)Host.Kotodama.EnvironmentTable["Base.撮影ToolStripMenuItem"];
            if (tsCapture != null)
            {
                tsCapture.Click +=new EventHandler(ts_Click);
            }
            tsAll = (ToolStripMenuItem)Host.Kotodama.EnvironmentTable["Base.全てを表示ToolStripMenuItem"];
            if (tsAll != null) 
            { 
                tsAll.Click+=new EventHandler(ts_Click);
            }
            tsFirst = (ToolStripButton)Host.Kotodama.EnvironmentTable["Base.toolStripButtonGoFirstView"];
            if (tsFirst != null)
            {
                tsFirst.Click+=new EventHandler(ts_Click);
            }
            tsRwd = (ToolStripButton)Host.Kotodama.EnvironmentTable["Base.toolStripButtonRwdView"];
            if (tsRwd != null)
            {
                tsRwd.Click +=new EventHandler(ts_Click);
            }            
            tsComboView = (ToolStripComboBox)Host.Kotodama.EnvironmentTable["Base.toolStripComboBoxView"];
            if (tsComboView != null)
            {
                tsComboView.SelectedIndexChanged += new EventHandler(viewComboBox_SelectedIndexChanged);
                tsComboView.Click +=new EventHandler(viewComboBox_Click);
            }
            tsFwd = (ToolStripButton)Host.Kotodama.EnvironmentTable["Base.toolStripButtonFwdView"];
            if (tsFwd != null)
            {
                tsFwd.Click +=new EventHandler(ts_Click);
            }
            tsLast = (ToolStripButton)Host.Kotodama.EnvironmentTable["Base.toolStripButtonGoLastView"];
            if (tsLast != null)
            {
                tsLast.Click+=new EventHandler(ts_Click);
            }
		}

        void ts_Click(object sender, EventArgs e)
        {
            if (sender == tsCapture || sender == tsButtonCapture)
            {
                Capture();
            }
            if (sender == tsEdit)
            {
                viewEditForm.UpdateList();
                viewEditForm.Show();
            }
            if (sender == tsRwd)
            {
                GoPrev();
            }
            if (sender == tsFwd)
            {
                GoNext();
            }
            if (sender == tsAll)
            {
                All_Click();
            }
            if (sender == tsFirst)
            {
                if (viewSnapList.Count > 0)
                {
                    currentViewIndex = 0;
                    bool succeeded = viewSnapList.Go(currentViewIndex);
                    if (succeeded && SnapChanged != null) SnapChanged(this, new SnapChangedEventArgs(currentViewIndex));
                }
            }
            if (sender == tsLast)
            {
                if (viewSnapList.Count > 0)
                {
                    currentViewIndex = viewSnapList.Count - 1;
                    bool succeeded = viewSnapList.Go(currentViewIndex);
                    if (succeeded && SnapChanged != null) SnapChanged(this, new SnapChangedEventArgs(currentViewIndex));
                }
            }
        }

		void PluginInterface.IPlugin.Dispose()
		{
			holder = null;
            ToolBarDock.ToolBarManager tbm = (ToolBarDock.ToolBarManager)Host.Kotodama.EnvironmentTable["Base.ToolBarManager"];
			if ( tbm != null)
			{
			    tbm.RemoveControl(toolBar);
				toolBar.ButtonClick -=new ToolBarButtonClickEventHandler(toolBar_ButtonClick);
			}
			else
			{// holderが無い ＞ menuについか
				defaultBar = (ToolBar)Host.Kotodama.EnvironmentTable["Base.ToolBar"];
				if(defaultBar != null)
				{
					defaultBar.Buttons.Remove(sepa);
					defaultBar.Buttons.Remove(buttonPrev);
					defaultBar.Buttons.Remove(buttonNext);
					defaultBar.ButtonClick-=new ToolBarButtonClickEventHandler(toolBar_ButtonClick);
				}

			}
            Host.Kotodama.Presentation.CurrentSlide.SlideLoaded -= new EventHandler(Presentation_CurrentSlideChanged);
			//Host.Kotodama.Presentation.CurrentSlideChanged-=new EventHandler(Presentation_CurrentSlideChanged);
			Host.Kotodama.KeyboardUpEventHandler-=new Qurihara.Keyboard.KeyboardUpEventHandler(Kotodama_KeyboardUpEventHandler);

			// NextPrev
			Host.Kotodama.Control.Controls.Remove(prevButton.Button);
			Host.Kotodama.Control.Controls.Remove(nextButton.Button);

            //ToolStrip main = (ToolStrip)Host.Kotodama.EnvironmentTable["Base.ToolStripMainMenu"];
            //if (main != null)
            //{
            //    main.Items.Remove(menu);
            //}

            //for new menu
            if (tsButtonCapture != null)
            {
                tsButtonCapture.Click -= new EventHandler(ts_Click);
            }
            if (tsEdit != null)
            {
                tsEdit.Click -= new EventHandler(ts_Click);
            }
            if (tsCapture != null)
            {
                tsCapture.Click -= new EventHandler(ts_Click);
            }
            if (tsAll != null)
            {
                tsAll.Click -= new EventHandler(ts_Click);
            }
            if (tsFirst != null)
            {
                tsFirst.Click -= new EventHandler(ts_Click);
            }
            if (tsRwd != null)
            {
                tsRwd.Click -= new EventHandler(ts_Click);
            }
            if (tsComboView != null)
            {
                tsComboView.SelectedIndexChanged -= new EventHandler(viewComboBox_SelectedIndexChanged);
                tsComboView.Click -= new EventHandler(viewComboBox_Click);
            }
            if (tsFwd != null)
            {
                tsFwd.Click -= new EventHandler(ts_Click);
            }
            if (tsLast != null)
            {
                tsLast.Click -= new EventHandler(ts_Click);
            }

		}

		public string Description
		{
			get
			{
				// TODO:  Add ctlMain.Description getter implementation
				return myPluginDescription;
			}
		}

		public string Author
		{
			get
			{
				// TODO:  Add ctlMain.Author getter implementation
				return myPluginAuthor;
			}
		}

		public IPluginHost Host
		{
			get
			{
				// TODO:  Add ctlMain.Host getter implementation
				return myPluginHost;
			}
			set
			{
				myPluginHost = value;
			}
		}

		
		public string Name
		{
			get
			{
				// TODO:  Add ctlMain.Name getter implementation
				return myPluginName;
			}
		}
		

		public string Version
		{
			get
			{
				// TODO:  Add ctlMain.Version getter implementation
				return myPluginVersion;
			}
		}

		#endregion


		private void Presentation_CurrentSlideChanged(object sender,EventArgs e)
		{
			//"default"を初期値としてセット。
			string p="default";
			//設定ファイルで記述しているならそれを取り込む
			//Util.GetStringProperty("MultiViewSnapPlugin.DefaultViewSnapList", ref p);
			Util.GetStringProperty("Base.InstanceId", ref p);
			viewSnapList = (ViewSnapList)Host.Kotodama.Presentation.CurrentSlide.ViewSnapListTable[p];

			//なにも当てはまるものが無い場合は、読み込んだテーブルの先頭のリストを用いる。
			if (viewSnapList == null && Host.Kotodama.Presentation.CurrentSlide.ViewSnapListTable.Count >0)
			{
				IEnumerator enumes = Host.Kotodama.Presentation.CurrentSlide.ViewSnapListTable.Values.GetEnumerator();
				enumes.MoveNext();
				viewSnapList = (ViewSnapList)enumes.Current;
			}
			if (viewSnapList != null)
			{
				UpdateViewSnapList();
			}
			else
			{
			}
		}
		private void UpdateViewSnapList()
		{
			viewSnapList.Invalidated+=new EventHandler(viewSnapList_Invalidated);
			viewEditForm.ViewSnapList = viewSnapList;

			ComboBox cb = viewEditForm.comboBoxListName;
			cb.Items.Clear();
			cb.Text = "";
			IDictionaryEnumerator enume = Host.Kotodama.Presentation.CurrentSlide.ViewSnapListTable.GetEnumerator();
			while(enume.MoveNext())
			{
				cb.Items.Add(enume.Value);
			}
			changedByClick = false;
			cb.SelectedIndex = cb.Items.IndexOf(viewSnapList);


			viewSnapList_Invalidated(this,new EventArgs());
			if(Host.Kotodama.EnvironmentTable.Contains("CurrentViewSnapList"))
			{
				Host.Kotodama.EnvironmentTable.Remove("CurrentViewSnapList");
			}
			Host.Kotodama.EnvironmentTable.Add("CurrentViewSnapList",viewSnapList);
		}

		private void viewSnapList_Invalidated(object sender, EventArgs e)
		{
			viewComboBox.Items.Clear();
			viewComboBox.Text = "";
			foreach(ViewSnap vs in viewSnapList)
			{
				viewComboBox.Items.Add(vs);
			}
			if (viewComboBox.Items.Count > 0)
			{
				if (currentViewIndex >= viewComboBox.Items.Count) currentViewIndex = viewComboBox.Items.Count-1;
				if (-1< currentViewIndex)
				{
					changedByClick = false;
					viewComboBox.SelectedIndex = currentViewIndex;
				}
			}

            if (tsComboView != null)
            {
                tsComboView.Items.Clear();
                tsComboView.Text = "";
                foreach (ViewSnap vs in viewSnapList)
                {
                    tsComboView.Items.Add(vs);
                }
                if (tsComboView.Items.Count > 0)
                {
                    changedByClick = false;
                    tsComboView.SelectedIndex = currentViewIndex;
                }
            }
		}

		private bool changedByClick = true;
		private void viewComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (changedByClick)
			{
                if (sender == viewComboBox)
                {
                    Go(viewComboBox.SelectedIndex);
                }
                if (tsComboView != null && sender == tsComboView)
                {
                    Go(tsComboView.SelectedIndex);
                }
			}
			changedByClick = true;
		}

		private void viewComboBox_Click(object sender, EventArgs e)
		{
			Go(currentViewIndex);
		}
		private void Go(int i)
		{
			int old = currentViewIndex;
			currentViewIndex = i;
			bool succeeded = viewSnapList.Go(currentViewIndex);
			if (!succeeded) currentViewIndex = old;
			if (succeeded && SnapChanged != null) SnapChanged(this,new SnapChangedEventArgs(currentViewIndex));
		}

		private void Kotodama_KeyboardUpEventHandler(object sender, KeyEventArgs e)
		{
			switch(e.KeyCode)
			{
				case Keys.F2:
				case Keys.PageDown:
					GoNext();
					break;
				case Keys.F1:
				case Keys.PageUp:
					GoPrev();
					break;
                case Keys.F4:
                    GoNextLinear();
                    break;
                case Keys.F3:
                    GoPrevLinear();
                    break;
                case Keys.F6:
                    GoNextWarp();
                    break;
                case Keys.F5:
                    GoPrevWarp();
                    break;
                default:
					break;
			}

		}

		#region NextPrev
		public void MenuClick(object o,System.EventArgs e)
		{
			enable = !enable;
			SetButtonLocation();
			prevButton.Button.Visible = enable;
			nextButton.Button.Visible = enable;

            prevButton.Button.BringToFront();
            nextButton.Button.BringToFront();
		}
		private void SetButtonLocation()
		{
			prevNomalRect = new Rectangle(0,
				Host.Kotodama.Control.ClientRectangle.Bottom-prevButton.Button.Height,//-32,
				50,50);
			nextNomalRect = new Rectangle(Host.Kotodama.Control.ClientRectangle.Right-nextButton.Button.Width,
				Host.Kotodama.Control.ClientRectangle.Bottom-nextButton.Button.Height,//-32,
				50,50);
			prevButton.Button.Location = prevNomalRect.Location;
			nextButton.Button.Location = nextNomalRect.Location;

			prevMagedRect = prevNomalRect;
			prevMagedRect.Y -= magnifySize;
			prevMagedRect.Width += magnifySize;
			prevMagedRect.Height += magnifySize;

			nextMagedRect = nextNomalRect;
			nextMagedRect.X -= magnifySize;
			nextMagedRect.Y -= magnifySize;
			nextMagedRect.Width += magnifySize;
			nextMagedRect.Height += magnifySize;

			prevButton.SetActionParam(100,prevNomalRect,prevMagedRect,500,RectMoveAnm.AnmType.Cubic);
			nextButton.SetActionParam(100,nextNomalRect,nextMagedRect,500,RectMoveAnm.AnmType.Cubic);

		}

		public void PrevClick(object o,System.EventArgs e)
		{
			GoPrev();
		}
		public void NextClick(object o,System.EventArgs e)
		{
			GoNext();
		}

		#endregion

		private void buttonListNew_Click(object sender, EventArgs e)
		{
			TextEntryForm tef = new TextEntryForm("新しい視点リストの作成","新しいリストの名前を入力してください");
			DialogResult dr =tef.ShowDialog();
			if (dr == DialogResult.OK)
			{
				if (Host.Kotodama.Presentation.CurrentSlide.ViewSnapListTable[tef.OutputText] == null)
				{
					ViewSnapList vs = new ViewSnapList(tef.OutputText);
					Host.Kotodama.Presentation.CurrentSlide.ViewSnapListTable.Add(vs.Id,vs);
					viewSnapList.Invalidated-=new EventHandler(viewSnapList_Invalidated);
					viewSnapList = vs;
					UpdateViewSnapList();
					viewEditForm.UpdateList();
				}
				else
				{
					MessageBox.Show("その名前のリストは既に存在します。");
				}
			}
			tef.Dispose();
		}

		private void buttonListDel_Click(object sender, EventArgs e)
		{
			if (Host.Kotodama.Presentation.CurrentSlide.ViewSnapListTable.Count > 1)
			{
				//ComboBox cb = viewEditForm.comboBoxListName;
				//ViewSnapList vsl = cb.Items[cb.SelectedIndex];
				viewSnapList.Invalidated-=new EventHandler(viewSnapList_Invalidated);
				Host.Kotodama.Presentation.CurrentSlide.ViewSnapListTable.Remove(viewSnapList.Id);

				IEnumerator enume = Host.Kotodama.Presentation.CurrentSlide.ViewSnapListTable.Values.GetEnumerator();
				enume.MoveNext();
				viewSnapList = (ViewSnapList)enume.Current;
				UpdateViewSnapList();
				viewEditForm.UpdateList();
			}
			else
			{
				MessageBox.Show("一つしかリストがないので消去できません。");
			}
		}

		private void cb_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (changedByClick)
			{
				viewSnapList.Invalidated-=new EventHandler(viewSnapList_Invalidated);
				ComboBox cb = viewEditForm.comboBoxListName;
				viewSnapList = (ViewSnapList)cb.Items[cb.SelectedIndex];
				UpdateViewSnapList();
				viewEditForm.UpdateList();
			}
			changedByClick = true;

		}

        public void GoPrevLinear()
        {
            currentViewIndex--;
            bool succeeded = viewSnapList.GoLinear(currentViewIndex);
            if (!succeeded) currentViewIndex++;
            if (succeeded && SnapChanged != null) SnapChanged(this, new SnapChangedEventArgs(currentViewIndex));
        }
        public void GoNextLinear()
        {
            currentViewIndex++;
            bool succeeded = viewSnapList.GoLinear(currentViewIndex);
            if (!succeeded) currentViewIndex--;
            if (succeeded && SnapChanged != null) SnapChanged(this, new SnapChangedEventArgs(currentViewIndex));
        }
        public void GoPrevWarp()
        {
            currentViewIndex--;
            bool succeeded = viewSnapList.GoWarp(currentViewIndex);
            if (!succeeded) currentViewIndex++;
            if (succeeded && SnapChanged != null) SnapChanged(this, new SnapChangedEventArgs(currentViewIndex));
        }
        public void GoNextWarp()
        {
            currentViewIndex++;
            bool succeeded = viewSnapList.GoWarp(currentViewIndex);
            if (!succeeded) currentViewIndex--;
            if (succeeded && SnapChanged != null) SnapChanged(this, new SnapChangedEventArgs(currentViewIndex));
        }


	}
}
